package com.inxedu.os.edu.controller.Images;
import com.inxedu.os.edu.pojo.EduWebsiteImagesType;
import com.inxedu.os.edu.service.images.ImagesService;
import com.inxedu.os.edu.service.images.ImageTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "媒体广告图类型")
public class Image_Type_Controller {
    @Autowired
    private ImagesService imagesService;
    @Autowired
    private ImageTypeService imageTypeService;

    //查询图片类型
    @RequestMapping(value = "/admin/imagetype/getlist",method = {RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "查询图片类型",notes = "查询图片类型")
    public ModelAndView getimageType(){
        ModelAndView model = new ModelAndView();
        model.setViewName("view/inxedu/admin/website/images/imageType-list");
        List<EduWebsiteImagesType> typelist = imagesService.getimagesType();model.addObject("typeList",typelist);
        return model;
    }
    //图片类型的删除
    @RequestMapping(value = "admin/imagetype/deletetype/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除图片类型",notes = "删除图片类型")
    @ApiImplicitParam(name = "id",dataType = "integer",value = "图片类型id")
    public ModelAndView delImageType(@PathVariable int id){
        ModelAndView model = new ModelAndView();
        int delImageType=imageTypeService.delImageType(id);
        if (delImageType!=0) {
            model.setViewName("redirect:/admin/imagetype/getlist");
        }
        return model;
    }
    @RequestMapping(value = "admin/imagetype/updateType",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "更改图片类型名称",notes = "更改图片类型名称")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "EduWebsiteImagesType",dataType = "EduWebsiteImagesType.java",value = "图片类型的封装对象"),
            @ApiImplicitParam(name = "typeId",dataType = "integer",value = "图片类型id"),
            @ApiImplicitParam(name = "typeName",dataType = "string",value = "图片类型的名称")})
    public Map<String,Object> updateType(EduWebsiteImagesType type,HttpServletRequest request){
        Map<String,Object> json = new HashMap<String,Object>();
        //获取页面id
        String typeid = request.getParameter("type.typeId");
        //获取页面typeName
        String typename = request.getParameter("type.typeName");
        int id = Integer.parseInt(typeid);
        type.setTypeId(id);
        type.setTypeName(typename);
        int updateImageTypeName=imageTypeService.updateImageTypeName(type);
        return json;
    }

    //创建图片类型
    @RequestMapping(value = "admin/imagetype/addtype",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    @ApiOperation(value = "创建图片类型",notes = "创建图片类型")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "EduWebsiteImagesType",dataType = "EduWebsiteImagesType.java",value = "图片类型的封装对象"),
                    @ApiImplicitParam(name = "typeId",dataType = "integer",value = "图片类型id"),
                    @ApiImplicitParam(name = "typeName",dataType = "string",value = "图片类型的名称")})
    public ModelAndView addType(EduWebsiteImagesType eduWebsiteImagesType){
        ModelAndView model = new ModelAndView();
        eduWebsiteImagesType.setTypeName("新建图片类型");
        imageTypeService.addImageType(eduWebsiteImagesType);
        model.setViewName("redirect:/admin/imagetype/getlist");
        return model;
    }
}
